// pages/video/video.js
import { getVideoList } from '../../api/video'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    videoList: [],
    hasMore: true
  },
  onLoad() {
    this.getVideoListData()
  },
  async getVideoListData() {
    const res = await getVideoList(this.data.page)

    this.setData({
      videoList: [...this.data.videoList, ...res.data.feeds]
    }, () => {
      if (this.data.videoList.length > 100) {
        this.setData({
          hasMore: false
        })
      }
    })
    wx.stopPullDownRefresh()
  },
  onPullDownRefresh() {
    this.data.page = 1
    this.setData({
      videoList: [],
      hasMore: true
    }, () => {
      this.getVideoListData()
    })
  },
  onReachBottom() {
    if (!this.data.hasMore) return

    this.data.page++
    this.getVideoListData()
  },
  goToVideoDetail(e) {
    wx.navigateTo({
      url: `/subpkg/video-detail/video-detail?id=${e.currentTarget.dataset.id}`,
    })
  }
})